package com.mall.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.mall.pageResult.PageResult;
import com.mall.service.PmsBrandService;
import com.mall.utils.Result;
import com.mall.pojo.PmsBrand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;

    //添加品牌
    @PostMapping("create")
    public Result create(@RequestBody PmsBrand pmsBrand) {
        pmsBrandService.create(pmsBrand);
        return Result.success(null, "");
    }

    //删除品牌
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        pmsBrandService.delete(id);
        return Result.success(null, "");
    }

    //更新品牌
    @PostMapping("/update/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody PmsBrand pmsBrand) {
        pmsBrandService.update(id, pmsBrand);
        return Result.success(null, "");
    }

    //根据编号查询品牌信息
    @GetMapping("/{id}")
    public Result<PmsBrand> selectById(@PathVariable("id") Integer id) {
        PmsBrand pmsBrand = pmsBrandService.selectById(id);
        return Result.success(pmsBrand, "");
    }

    //批量删除品牌
    @PostMapping("/delete/batch")
    public Result batch(@RequestParam("ids") List<Integer> ids) {
        pmsBrandService.batch(ids);
        return Result.success(null, "");
    }

    //批量更新显示状态
    @PostMapping("/update/showStatus")
    public Result showStatus(@RequestParam("ids") List<Integer> ids,
                             @RequestParam("showStatus") Integer showStatus) {
        pmsBrandService.showStatus(ids, showStatus);
        return Result.success(null, "");
    }

    //批量更新厂家制造商状态
    @PostMapping("/update/factoryStatus")
    public Result factoryStatus(@RequestParam("ids") List<Integer> ids,
                                @RequestParam("factoryStatus") Integer factoryStatus) {
        pmsBrandService.factoryStatus(ids, factoryStatus);
        return Result.success(null, "");
    }

    //获取全部品牌列表
    @GetMapping("/listAll")
    public Result listAll() {
        List<PmsBrand> list = pmsBrandService.listAll();
        return Result.success(list, "");
    }

    //根据品牌名称分页获取品牌列表
    @GetMapping("/list")
    public Result<PageResult<PmsBrand>> list(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                             @RequestParam(value = "keyword", required = false) String keyword) {
        PageResult<PmsBrand> pageResult = new PageResult<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        Page<PmsBrand> page = pmsBrandService.list(pageSize, pageNum, keyword);
        if (CollUtil.isEmpty(page.getRecords())) {
            pageResult.setList(new ArrayList<>());
            return Result.success(pageResult, "操作成功");
        }
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        pageResult.setList(page.getRecords());
        return Result.success(pageResult, "操作成功");
    }


}
